import { Pool } from '@neondatabase/serverless';

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
});

export async function query(text, params) {
  return await pool.query(text, params);
}

export async function getAvailableSlots(date) {
  const res = await pool.query(
    'SELECT time_slot FROM appointments WHERE date = $1', 
    [date]
  );
  return res.rows;
}

export async function createAppointment(appointment) {
  const { name, phone, date, time_slot, contact_method, notes } = appointment;
  
  const res = await pool.query(
    `INSERT INTO appointments 
     (name, phone, date, time_slot, contact_method, notes) 
     VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`,
    [name, phone, date, time_slot, contact_method, notes]
  );
  
  return res.rows[0];
}